<?php
namespace app\admin\controller;
use think\Db;
use think\Loader;

/**
 * 栏目列表.
 * User: Administrator
 * Date: 2017/10/28
 * Time: 15:46
 */
class Cata extends Base
{
    /*栏目列表*/
    public function ls(){
        $cata = new \data\model\Cata();
        $res =  $cata->paginate(10);
        $this->assign('res',$res);
        return $this->fetch('admin/cata/ls');
    }
    /*添加栏目*/
    public function add(){
        if(request()->post()){
            $data=[
                'cataname' =>input('cataname')
            ];
            $cata = new \data\model\Cata();
            $rs = $cata->validate('cata')->save($data);
            if(false === $rs){
                $this->error($cata->getError());die();
            }
            if($rs){
                $this->success('添加成功','ls');
            }else{
                $this->error('添加失败请重新添加');
            }
        }
        return $this->fetch('admin/cata/add');
    }
    /*修改栏目*/
    public function edit(){
        $cataid = input('id');
        $cata = \data\model\Cata::get(['cataid'=>$cataid]);
        $this->assign('cata',$cata);
        if(request()->post()){
            $data=[
                'cataid' =>input('id'),
                'cataname' =>input('cataname')
            ];
            $cata = new \data\model\Cata();
            $res = $cata->validate('cata')->isUpdate()->save($data);
            if(false === $res){
                $this->error($cata->getError());die();
            }
            if($res){
                $this->success('修改成功','ls');
            }else{
                $this->error('修改失败请重新修改');
            }
        }
        return $this->fetch('admin/cata/edit');
    }
    /*删除栏目*/
    public  function del(){
        $id = input('id');
        if(\data\model\Cata::destroy($id)){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}